const {ClockInUrl, HabitUrl } = require("../../../config/config")

Page({
  data: {
    currentIndexNav:0,
    myHabits:[],
    hid:0,
    clockins:[],
    date:''

  },
  onLoad: function (options) {
    let _this = this
    let myDate = new Date();
    let year = myDate.getFullYear();
    let month = myDate.getMonth()+1;
    if(month < 10){
      month = '0'+month
    }
    let dateStr = year +'-'+ month;
    this.setData({
        date : dateStr       
    })
    wx.request({
      url: HabitUrl + '/list_mine/'+wx.getStorageSync('userinfo').id,
      method: 'GET', 
      success: function(res){
        _this.setData({
          myHabits : res.data.result,
          hid : res.data.result[0].id    
        })
        _this.listCanlendar()
      }
    })


    
  },
  activeNav:function(e){
    let _this = this 
    this.setData({
      currentIndexNav: e.target.dataset.index,
      hid: _this.data.myHabits[e.target.dataset.index].id
    })
  },
  contentChange:function(e){
    let _this = this
    this.setData({
      currentIndexNav: e.detail.current,
      hid: _this.data.myHabits[e.detail.current].id
    })
    this.listCanlendar()
  },
  next: function (event) {
    let year = event.detail.currentYear
    let month = event.detail.currentMonth
    if(month < 10){
      month = '0'+month
    }
    let dateStr = year +'-'+ month;
    this.setData({
        date : dateStr       
    })
    this.listCanlendar()
  },
  prev: function (event) {
    let year = event.detail.currentYear
    let month = event.detail.currentMonth
    if(month < 10){
      month = '0'+month
    }
    let dateStr = year +'-'+ month;
    this.setData({
        date : dateStr       
    })
    this.listCanlendar()
  },
  listCanlendar:function(){
    let _this = this
    wx.request({
      url: ClockInUrl +'/list_month',
      data: {
        hid : _this.data.hid,
        date: _this.data.date
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' 
      },
      method: 'POST', 
      success: function(res){
        let dateItems = res.data.result
        let objs = []
        for(let i=0; i<dateItems.length; i++){
          let obj = {
            month:"current",
            day:"",
            color:"#ffffff",
            background:"#6887E2"
          }
          obj.day = dateItems[i]
          objs.push(obj)
        }
        _this.setData({
          clockins : objs
        })
      }
    }) 
  },
})